package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.QueryAdminDTO;
import com.woniu.pojo.Admin;
import com.woniu.mapper.AdminMapper;
import com.woniu.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaoruan
 * @since 2023-03-10
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Resource
    AdminMapper adminMapper;

    @Override
    public List<String> findAllUserName() {
     List<String> userNames = adminMapper.findAllUserNames();
        return userNames;
    }

    @Override
    public PageData getPageData(Integer currentPage, Integer pageSize, QueryAdminDTO queryAdminDTO) {
        Page<Admin> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(queryWrapper)) {
            if (!StringUtils.isEmpty(queryAdminDTO.getUserName())) {
                queryWrapper.like(Admin::getUserName, queryAdminDTO.getUserName());
            }
            if (!StringUtils.isEmpty(queryAdminDTO.getIsAdmin())) {
                queryWrapper.eq(Admin::getIsAdmin, queryAdminDTO.getIsAdmin());
            }
            if (!StringUtils.isEmpty(queryAdminDTO.getCreateTimeStart())) {
                queryWrapper.ge(Admin::getCreateTime, queryAdminDTO.getCreateTimeStart());
            }

            if (!StringUtils.isEmpty(queryAdminDTO.getCreateTimeEnd())) {
                queryWrapper.le(Admin::getCreateTime, queryAdminDTO.getCreateTimeEnd());
            }
        }

        Page<Admin> adminPage = this.page(page, queryWrapper);
        List<Admin> records = adminPage.getRecords();
        long total = adminPage.getTotal();

        PageData pageData = new PageData();
        pageData.setTotal(total);
        pageData.setData(records);


        return pageData;
    }
}
